﻿Imports Microsoft.VisualBasic.Compatibility.VB6

Public Class FRMmain

    Public aColumnWidths As Object

    Public _
        bRemoteSaved As Boolean

    Public _
        uniqueIDindex As Integer

    Private _
        doRemoteUpdate As Boolean, _
        boolAddrow As Boolean

    Dim _
        matchPhrase As String, _
        matchCase As String, _
        strColumnWidths

    Dim _
        Ymouse As Integer, _
        Xmouse As Integer


    Dim fso As Scripting.FileSystemObject

    ' Form load event -------------------------------------------------------------------------------------------------------------------------------

    Private Sub Form_Load()

        On Error Resume Next

        bRemoteSaved = True

        ' Set display properties ----------------------------------------------------------------------------------------

        editMenu.Visible = False

        MODdbMethods.init()
        MODweb.init()

        initPreferences()

        ' Check that remote update directory exists and create it if it doesn't. ---------------------------------------------------------------

        fso = New Scripting.FileSystemObject

        If Not fso.FolderExists(MODweb.sRemoteUpdateDirPath) Then fso.CreateFolder(MODweb.sRemoteUpdateDirPath)
        If Not fso.FolderExists(MODweb.sRemoteUpdateBackupDirPath) Then fso.CreateFolder(MODweb.sRemoteUpdateBackupDirPath)

    End Sub


    ' Form unload ------------------------------------------------------------------------------------------------------------------

    Private Sub Form_Unload(ByVal Cancel As Integer)

        On Error Resume Next

        removeFocusFromGrid()

        If Not bRemoteSaved Then

            If MsgBox( _
                "You have made changes to the local database, but have not updated the remote database" & _
                vbCrLf & vbCrLf & _
                "Is this OK ?", _
                vbOKCancel, _
                "Unsaved Changes") <> 1 Then

                Cancel = 1

            End If
        End If

        If Cancel = 0 Then End

    End Sub

    ' Set the widths of the dataGrid columns according to the registry preferences -----------------------------------------------------------------------------------

    Public Sub setDataGridColumnWidths()

        ' Get preferences from registry --------------------------------------------------------------------------------

        Throw New NotImplementedException

    End Sub

    Private Sub editPreferences_Click()

        DLGPreferences.Show()

    End Sub

    Private Sub editAddRow_Click()

        On Error GoTo errorHandler

        boolAddrow = True
        'Adodc1.Recordset.AddNew()

        '' SET A DEFAULT VALUE FOR ALL FIELDS
        'For Each objField In Adodc1.Recordset.Fields
        'objField.Value = 0
        'Next

        'uniqueID = "part_" & getUniqueTimeStamp() & "_" & uniqueIDindex
        uniqueIDindex = uniqueIDindex + 1
        'Adodc1.Recordset("uniqueID") = uniqueID
        isNewRecord = True
        'MODweb.buildUpdateStatement(Adodc1.Recordset, "INSERT")

        Exit Sub

errorHandler:

        If Err.Number = 91 Then MsgBox("There is no open DataBase")

        'debug.print Err.Number
    End Sub

    Private Sub editCopyRow_Click()

        On Error Resume Next

        copyRow()

    End Sub

    Private Sub editDeleteRow_Click()
        Throw New NotImplementedException
        '        On Error GoTo errorHandler

        '        MODweb.buildUpdateStatement(Adodc1.Recordset, "DELETE")

        '        FRMmain.TXTsearch.Focus() ' this takes the focus away from the datagrid and prevents an error message from the datagrid

        '        Adodc1.Recordset.Delete()

        'errorHandler:
        '        If Err.Number = 91 Then MsgBox("There are no rows to delete")
        'debug.print Err.Number
    End Sub

    Private Sub initPreferences()

        On Error Resume Next

        With New regOp

            .Root = regOp.RegRoot.HKEY_LOCAL_MACHINE
            .Key = "Software\bbd\AccessSearch"

            '======================================================================================='
            ' Install Initial Values in the registry, in case they do not exist already             '
            '======================================================================================='
            setDefaultPrefs()

        End With

    End Sub

    '-------------------------------------------'
    '   copy row from dataGrid to clipboard     '
    '-------------------------------------------'
    Private Sub copyRow()
        Throw New NotImplementedException
        '        On Error GoTo handleError

        '        Dim hasRecords As Boolean
        '        For Each c In DataGrid.Columns
        '            If c.Text <> "" Then
        '                hasRecords = True
        '                Exit For
        '            End If
        '        Next
        '        If hasRecords Then
        '            Dim rowVal As String, headers As String, i As Integer
        '            selVal = ""
        '            Dim rs As Recordset
        '            rs = FRMmain.Adodc1.Recordset
        '            For Each fld In rs.Fields
        '                headers = headers & "|" & fld.Name
        '            Next
        '            For Each c In DataGrid.Columns
        '                rowVal = rowVal & "|" & c.Text
        '                i = i + 1
        '            Next
        '            Dim objDoc As Word.Document
        '            objDoc = New Word.Document
        '            objDoc.Activate()
        '            objDoc.Tables.Add(objDoc.Range, 2, i)
        '            splHeaders = Split(headers, "|")
        '            For i = 0 To UBound(splHeaders)
        '                objDoc.Tables(1).Cell(1, i + 1).Range = splHeaders(i)
        '            Next
        '            splRowVal = Split(rowVal, "|")
        '            For i = 0 To UBound(splRowVal)
        '                objDoc.Tables(1).Cell(2, i + 1).Range = splRowVal(i)
        '            Next
        '            Clipboard.Items.Clear()
        '            objDoc.Range.Copy()
        '            objDoc.Close(False)
        '            objDoc = Nothing
        '        End If

        'handleError:
        '        'debug.print Err.Number
        '        If Err.Number = 7005 Then MsgBox("There are no rows to copy")
        '        Exit Sub
    End Sub
    '---------------------------------------'
    '   delete a row in the dataGrid          '
    '---------------------------------------'
    Private Sub deleteRow()
        Throw New NotImplementedException
        'On Error Resume Next

        'Dim deleteStr As String

        'Dim rs As ADODB.Recordset
        'rs = Adodc1.Recordset

        'deleteStr = "DELETE * FROM " & Me.LISTtables.Items(Me.LISTtables.SelectedIndex) & " WHERE [" & _
        '    rs.Fields(0).Name & "] = '" & rs.Fields(0).Value & "'"

        'For i = 1 To rs.Fields.Count - 1
        '    deleteStr = deleteStr & " AND [" & rs.Fields(i).Name & "] = '" & rs.Fields(i).Value & "'"
        'Next
        'rs.Close()
        'Adodc1.RecordSource = deleteStr
        'MsgBox(deleteStr)
        'rs.Open deleteStr
        'DataGrid.Refresh
handleError:
        Exit Sub
    End Sub
    '---------------------------------------'
    '   Menu > Import click event           '
    '---------------------------------------'
    'Private Sub fileImport_Click()
    'On Error GoTo handleError
    '    CommonDialog.ShowOpen
    '    With CommonDialog
    '        DLGimport.Show
    '        DLGimport.TEXTimportFileName.Text = .FileName
    '        MODdbMethods.setImportTablesCombo .FileName
    '    End With
    'handleError:
    '    If Err.Number = -2147217842 Then
    '        DLGimport.Hide
    '        Exit Sub
    '    End If
    'End Sub
    '---------------------------------------'
    '   Menu > Export Click event           '
    '---------------------------------------'
    Private Sub fileExportToExcel_Click()

        On Error Resume Next

        If LISTtables.SelectedIndex < 0 Then

            MsgBox("There is no open database to export")

            Exit Sub

        End If

        DLGExport.Show(Me)

    End Sub
    '---------------------------------------'
    '   Menu > Open Click event             '
    '---------------------------------------'
    Private Sub fileOpen_Click()
        Throw New NotImplementedException
        '        On Error GoTo errorHandler

        '        With CommonDialog

        '            .Filter = "Access Databases (*.mdb)|*.mdb|All Files|*.*"

        '            .CancelError = True

        '            .ShowOpen()

        '            MODdbMethods.openConnection.FileName()

        '        End With

        'errorHandler:

        '        Select Case Err.Number

        '            Case 32755 ' Cancel was selected

        '                Exit Sub

        '        End Select

    End Sub


    Private Sub filePrint_Click()
        Throw New NotImplementedException
        '        On Error Resume Next

        '        FRMgrid.GRIDresults.Items.Clear()
        '        FRMgrid.GRIDresults.Rows = 1
        '        FRMgrid.GRIDresults.Cols = 0
        '        intRows = 1
        '        intCols = 1
        '        On Error GoTo printErrorHandler
        '        Printer.Orientation = 2
        '        Printer.PaintPicture(DataGrid.CaptureImage, 0, 0)
        '        Printer.EndDoc()
        '        Exit Sub

        'printErrorHandler:
        '        If Err.Number = 30010 Then
        '            MsgBox(Err.Description) ' "There Are No Results To Print"
        '        Else
        '            MsgBox("Error : " & Err.Description & vbCrLf & "Error Number : " & Err.Number)
        '        End If
        '        Exit Sub

    End Sub


    Private Sub searchButt_Click()

        On Error Resume Next

        If LISTtables.SelectedIndex > -1 Then

            If TXTSearch.Text <> "" Then

                If ListFields.SelectedIndices.Count > 0 Then

                    Dim queryStr As String
                    queryStr = Trim(TXTSearch.Text)
                    MODdbMethods.searchDB(LISTtables.Items(LISTtables.SelectedIndex), ListFields.Items(ListFields.SelectedIndex), queryStr)

                Else

                    MsgBox("Choose At Least One Field To Search, or Choose ""All""")

                End If
            Else

                MsgBox("Please Choose A Phrase To Search For")
                Me.TXTSearch.Focus()
            End If
        Else
            MsgBox("No File Open")
            TXTSearch.Focus()
        End If

        setDataGridColumnWidths()

    End Sub

    Private Sub LISTtables_Click()

        On Error Resume Next

        If LISTtables.SelectedIndex > -1 Then
            ''''''''''''' CREATE FIELDS LIST BOX and COLUMN HEADERS FOR SEARCH RESULTS
            Dim sSelectedField As String

            sSelectedField = getPref("", "DefaultSearchField")

            MODdbMethods.setFieldsList(LISTtables.Items(LISTtables.SelectedIndex), sSelectedField)

        End If

    End Sub

    Private Sub fileQuit_Click()

        End

    End Sub



    ' Take the focus away from the last edited row so it is forced to update -------------------------------------------------------------------------

    Private Sub removeFocusFromGrid()
        Throw New NotImplementedException
        'On Error Resume Next

        'If DataGrid.Row = 0 Then

        '    DataGrid.Row = 1

        'Else

        '    DataGrid.Row = 0

        'End If

    End Sub

    ' Events -------------------------------------------------------------------------------------------------------------------------

    ' Import File ---------------------------------------------------------------------------------------------------------------------

    Public Sub fileImport_Click()
        Throw New NotImplementedException
        'On Error Resume Next

        'With CommonDialog

        '    .Filter = "Excel Files (.xls)|*.xls|All Files|*.*"

        '    .CancelError = True

        '    .ShowOpen()

        '    DLGimport.init.FileName()

        'End With

    End Sub

    ' Web menu events ---------------------------------------------------------------------------------------------------------------

    Private Sub WebDeleteSavedUpdate_Click()

        On Error Resume Next

        Dim fso As Scripting.FileSystemObject

        fso = New Scripting.FileSystemObject

        If Not fso.FileExists(MODweb.sRemoteUpdateRecordsFilePath) Then

            MsgBox("There are no records to delete")

            Exit Sub

        End If

        If MsgBox("Are you sure you want to delete the records ?", vbOKCancel) <> vbOK Then Exit Sub

        fso.CopyFile( _
            MODweb.sRemoteUpdateRecordsFilePath, _
            MODweb.sRemoteUpdateBackupDirPath & "records_" & getUniqueTimeStamp())

        fso.DeleteFile(MODweb.sRemoteUpdateRecordsFilePath, False)

        fso = Nothing

    End Sub

    Private Sub WebUpdateAllRecords_Click()

        On Error Resume Next

        removeFocusFromGrid()

        updateAllRecords()

    End Sub

    Private Sub WebUpdateFromFile_Click()

        On Error Resume Next

        MODweb.updateFromFile()

    End Sub

    Private Sub WebUpdateLater_Click()

        On Error Resume Next

        removeFocusFromGrid()

        MODweb.exportRecordsForUpdate()

    End Sub

    ' Tool menu events --------------------------------------------------------------------------------------------------------

    Private Sub toolsCalculator_Click()

        On Error Resume Next

        Dim X As Long
        X = Shell("Calc.exe", 1)

    End Sub

    Private Sub ToolSendMail_Click()

        MODtools.sendMailToBroker()

    End Sub


    Private Sub toolsPreferences_Click()

        On Error Resume Next

        DLGPreferences.Show(Me)

    End Sub

    ' Data grid events ---------------------------------------------------------------------------------------------------------------

    Private Sub DataGrid_AfterColEdit(ByVal ColIndex As Integer)
        Throw New NotImplementedException
        'On Error Resume Next

        'If doRemoteUpdate Then

        '    isNewRecord = False
        '    MODweb.buildUpdateStatement(Adodc1.Recordset, "SET")

        'End If

    End Sub

    Private Sub DataGrid_BeforeColUpdate(ByVal ColIndex As Integer, ByVal OldValue As Object, ByVal Cancel As Integer)
        Throw New NotImplementedException
        'On Error Resume Next

        'doRemoteUpdate = True
        'Dim newValue As Object

        '' Check to make sure that the user does not update the 'uniqueID' field ----------------------------------------------------------------
        '' If the condition returns true thrn cancel the update --------------------------------------------------------------------------

        'If Adodc1.Recordset.Fields(ColIndex).Name = "uniqueID" Then

        '    Cancel = 1
        '    doRemoteUpdate = False

        '    Exit Sub

        'End If

        'MODweb.buildUpdateStatement(Adodc1.Recordset, "WHERE")

    End Sub

    Private Sub DataGrid_BeforeUpdate(ByVal Cancel As Integer)

        On Error Resume Next
        Throw New NotImplementedException
        'Adodc1.Recordset("UPDATE") = Date & " " & Time

    End Sub

    Private Sub DataGrid_Mousedown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        Throw New NotImplementedException
        'On Error Resume Next

        'If Button And vbRightButton Then

        '    Dim selectedRow As Integer, Xtwips As Integer, Ytwips As Integer

        '    If Screen.TwipsPerPixelX < 1 Then Xtwips = 1 Else Xtwips = Screen.TwipsPerPixelX
        '    If Screen.TwipsPerPixelY < 1 Then Ytwips = 1 Else Ytwips = Screen.TwipsPerPixelY

        '    If DataGrid.ColContaining(X / Xtwips) > -1 Then

        '        Exit Sub

        '    End If

        '    selectedRow = DataGrid.RowContaining(Y / Ytwips)
        '    DataGrid.Row = selectedRow
        '    PopupMenu(editMenu)

        'End If

    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class